const mongoose = require('mongoose')
const Schema = mongoose.Schema
const { ObjectId, Mixed }= Schema.Types //Mixed可以存储任何类型的数据

const movieSchema = new Schema({
	doubanId: {
		unique: true,
		type: String
	},

	category: [{
		type: ObjectId,
		ref: 'Category'
	}],

	rate: Number,
	title: String,
	summary: String,
	video: String,
	poster: String,
	cover: String,

	videoKey: String,
	posterKey: String,
	coverKey: String,

	rawTitle: String,
	movieTypes: [String],
	pubDate: Mixed,
	year: Number,

	tags: [String],

	meta: {
		createdAt: {
			type: Date,
			default: Date.now()
		},
		updatedAt: {
			type: Date,
			default: Date.now()
		}
	}
})
//mongoDB在save之前的一个中间件的操作
movieSchema.pre('save', function(next) {
	if (this.isNew) {
		this.meta.createdAt = this.meta.updatedAt = Date.now()
	} else {
		this.meta.updatedAt = Date.now()
	}
	next()
})
mongoose.model('Movie', movieSchema)